/** @file    ResourcePath.cpp
 *  @author  Alessandro Polo
 *  @version $Id: ResourcePath.cpp 2831 2010-08-02 22:15:14Z alex $
 *  @brief
 * File containing methods for the wosh::ResourcePath class.
 * The header for this class can be found in ResourcePath.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <core/ResourcePath.h>
 #include <core/ObjectAllocator.h>
 #include <core/FilePath.h>
 #include <core/Utilities.h>


using namespace std;
namespace wosh {

 WOSH_REGISTER(wosh::ResourcePath, "", 1.01, _static_ResourcePath)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

ResourcePath::ResourcePath( const std::string& fullpath ) {
	set(fullpath);
 }

ResourcePath::ResourcePath( const std::string& Path, const std::string& Protocol ) {
	setPath(Path);
	setProtocol(Protocol);
 }

ResourcePath::ResourcePath( const ResourcePath& m ) {
	*this = m;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::string ResourcePath::resolve( bool omit_protocol_prefix ) const {
	 if ( !this->protocol.empty() && this->protocol != _ResourcePath_ProtocolFile ) {
		if ( omit_protocol_prefix )
			return this->path;
		return getPathFull();
	 }
	std::string temp = this->path;
	FilePath::resolve( temp );
	FilePath::toLocalPath( temp );
	if ( omit_protocol_prefix )
		return temp;
	return this->protocol + _ResourcePath_ProtocolTerminator + temp;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool ResourcePath::set( const std::string& full_path ) {
	std::string temp = full_path;
	Utilities::trimSpaces(temp);
	if ( temp.empty() ) return false;
	this->protocol = "";
	size_t pos = temp.find(_ResourcePath_ProtocolTerminator);
	if ( pos == std::string::npos )
		this->path = temp;
	else {
		this->protocol = temp.substr(0, pos);
		this->path = temp.substr( pos + strlen(_ResourcePath_ProtocolTerminator) );
	 }
	Utilities::trimSpaces(this->path);
	if ( this->path.size() == 0 )
		return false;
	Utilities::trimSpaces(this->protocol);
	if ( this->protocol.empty() )
		this->protocol = _ResourcePath_ProtocolDefault;
	return true;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

ResourcePath& ResourcePath::operator=(const ResourcePath& m) {
	if (this == &m) return *this; // same object?
	this->protocol = m.protocol;
	this->path = m.path;
	return *this;
 }

bool ResourcePath::operator==(const ResourcePath &other) const {
	if ( this->protocol != other.protocol )
		return false;
	if ( this->path != other.path )
		return false;
	return true;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool ResourcePath::updateHash( DataHashContext* hash_context ) const {
	if ( hash_context == NULL ) return false;
	bool ret = true;
	ret = hash_context->update( this->protocol ) & ret;
	ret = hash_context->update( this->path ) & ret;
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

}; // namespace wosh
